package cn.wolfcode.ssm.web.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;
import cn.wolfcode.ssm.service.IPermissionService;
import cn.wolfcode.ssm.service.IRoleService;
import cn.wolfcode.ssm.service.IRoleService;
import cn.wolfcode.ssm.util.RequireAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("list")
    public String listAll(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "role/list";
    }

    @RequireAnnotation(name = "角色删除",expression = "role:delete")
    @RequestMapping("delete")
    public String delete(Long id){
        roleService.deleteById(id);
        return "redirect:/role/list";
    }

    @RequireAnnotation(name = "角色编辑/保存",expression = "role:saveOrUpdate")
    @RequestMapping("input")
    public String input(Model model,Long id){
        if(id != null){
            Role role = roleService.selectById(id);
            model.addAttribute("role",role);
        }
        model.addAttribute("permissions",permissionService.listAll());
        model.addAttribute("roles",roleService.listAll());
        return "role/input";
    }

    @RequireAnnotation(name = "角色编辑/保存",expression = "role:saveOrUpdate")
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Model model,Role role,Long[] ids){
        if(role.getId() != null){
            roleService.updateById(role,ids);
        }else {
            roleService.save(role,ids);
        }
        return "redirect:/role/list";
    }

}
